package xyz.labmem.base.core.tool

import cn.hutool.core.util.StrUtil
import java.util.*
import java.util.regex.Pattern


/**
 * @Author lty
 * @Date 2022/1/18 10:58
 */

fun String?.isNotBlank(): Boolean = StrUtil.isNotBlank(this)

fun String?.isBlank(): Boolean = StrUtil.isBlank(this)

fun String?.contains(searchStr: String): Boolean = StrUtil.contains(this, searchStr)

fun String?.isEmpty(): Boolean = StrUtil.isEmpty(this)

/**
 * 驼峰转下划线
 */
fun String.XH(): String {
    val humpPattern = Pattern.compile("[A-Z]")
    val matcher = humpPattern.matcher(this)
    val sb = StringBuilder()
    while (matcher.find()) {
        matcher.appendReplacement(sb, "_" + matcher.group(0).lowercase(Locale.getDefault()))
    }
    matcher.appendTail(sb)
    return sb.toString()
}

/**
 * jackson大小写序列化手动版
 */
fun String.jacksonSerializationLU(): String {
    if(this.isUpperCase() == false){
        return this
    }
    var index = -1
    run outside@{
        this.split("").forEachIndexed { i, s ->
            if (s !in "a".."z" && s !in "A".."Z" && s != "") {
                index = i
                return@outside
            }
        }
    }
    return if (index > -1) {
        this.substring(0, index).lowercase() + this.substring(index)
    } else
        this.lowercase()
}

/**
 * 驼峰式命名法 例如：user_name->userName
 */
fun String.toCamelCase(): String? {
    if(this.isUpperCase() == false){
        return this
    }
    var s = this
    s = s.lowercase(Locale.getDefault())
    val sb = StringBuilder(s.length)
    var upperCase = false
    for (i in 0 until s.length) {
        val c = s[i]
        if (c == '_') {
            upperCase = true
        } else if (upperCase) {
            sb.append(c.uppercaseChar())
            upperCase = false
        } else {
            sb.append(c)
        }
    }


    return sb.toString()
}

fun String.isUpperCase(): Boolean? {
    return Character.isUpperCase(this.get(0))
}